package com.xhorse.lockplatform.manage.controller;

import com.xhorse.lockplatform.comm.constant.CommonResult;
import com.xhorse.lockplatform.comm.enums.ResultEnum;
import com.xhorse.lockplatform.comm.util.LogUtil;
import com.xhorse.lockplatform.model.XhProduct;
import com.xhorse.lockplatform.service.XhProductService;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author Think
 */
@Controller("xhProductController")
@RequestMapping(value = "/api")
public class XhProductController {

    @Resource
    private XhProductService xhProductService;

    @ResponseBody
    @RequestMapping(value = "/xh_product/list", method = RequestMethod.GET)
    public CommonResult findXhProductList(HttpServletRequest request,
                                          HttpServletResponse response) {
        response.setContentType("text/json;charset=UTF-8");
        try {
            List<XhProduct> list = xhProductService.listAll();
            return new CommonResult(list);
        } catch (DuplicateKeyException e) {
            LogUtil.getLogger().error("save loanOrder error :" + e.getMessage());
            return new CommonResult(ResultEnum.FAILED, "此订单号已经存在");
        }
    }

}
